package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addBService;

//    新增地址
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook){
        Integer save = addBService.save(addressBook);
        if(save != null){
            return Result.success();
        }
        return Result.error("新增地址失败");
    }

//    查询当前登录用户的所有地址信息
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        List<AddressBook> all = addBService.getAll();
        return Result.success(all);
    }

//    查询默认地址
    @GetMapping("/default")
    public Result<AddressBook> selectDefaultAdd(){
        AddressBook AddDefault = addBService.getDefault();
        return Result.success(AddDefault);
    }


//    根据id修改地址
    @PutMapping
    public Result updateById(@RequestBody AddressBook addressBook){
        Integer integer = addBService.updateById(addressBook);
        if(integer == 0){
            return Result.error("失败");
        }
        return Result.success();
    }

//    根据id删除地址
    @DeleteMapping
    public Result deleteById(Long id){
        Integer integer = addBService.deleteById(id);
        if(integer == 0){
            return Result.error("失败");
        }
        return Result.success();
    }


//    根据id查询地址
    @GetMapping("/{id}")
    public Result<AddressBook> seleteById(@PathVariable Long id){
        AddressBook addressBook = addBService.seleteById(id);
        if(addressBook != null){
            return Result.success(addressBook);
        }
        return Result.error("失败");
    }

//    设置默认地址
    @PutMapping("/default")
    public Result  updateDefault(@RequestBody AddressBook addressBook){
        Integer integer = addBService.updateDefault(addressBook);
        if(integer != null){
            return Result.success();
        }
        return Result.error("失败");
    }

}
